﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ninject;
using Mabalacat.CPS.BusinessLogic.Services.Interfaces;
using Mabalacat.CPS.DataAccess.DataObjects;
using Web.Codes;

namespace Web.Admin
{
    public partial class UserEntry : BasePage //System.Web.UI.Page
    {
        [Inject]
        public IUserService _userService { get; set; }
        private User _currentUser;
        public User CurrentUser 
        {
            get
            {
                if (_currentUser == null && Id > 0)
                {
                    _currentUser = _userService.GetById(Id);                    
                }
                return  _currentUser ?? new User();                
            }
            set { _currentUser = value; }
        }
        public bool IsSuccess { get; set; }
        public bool InvalidUsername 
        {
            get 
            { 
                return Page.IsPostBack && string.IsNullOrEmpty(UserNameTxt.Value); 
            } 
        }
        public bool InvalidPassword 
        { 
            get 
            {
                return Page.IsPostBack && string.IsNullOrEmpty(PassWordTxt.Value) || PassWordTxt.Value != ConfirmPassWordTxt.Value; 
            } 
        }
        private int _id;
        public int Id
        {
            get { return _id; }
            set { _id = value; }
        }

        protected override void OnInit(EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["id"]))
                int.TryParse((string)Request.QueryString["id"], out _id);                                
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
                showDetails();
        }

        #region Implementations
        void addNew()
        {
            CurrentUser = new User
            {
                UserName = UserNameTxt.Value,
                Password = PassWordTxt.Value
            };
            _userService.Add(CurrentUser);            
        }
        void update()
        {
            CurrentUser.UserName = UserNameTxt.Value;
            CurrentUser.Password = PassWordTxt.Value;

            _userService.Update(CurrentUser);            
        }
        void showDetails()
        {
            UserNameTxt.Value = CurrentUser.UserName;
            PassWordTxt.Value = CurrentUser.Password;
        }
        #endregion

        protected void SubmitBtn_Click(object sender, EventArgs e)
        {
            if (InvalidUsername || InvalidPassword)
                return;
           
            if (CurrentUser.Id > 0)
            {
                 update();                
            }
            else
            {
                 addNew();
            }
            IsSuccess = _userService.Save();

        }
        protected void AddNewBtn_Click(object sender, EventArgs e)
        {            
            Response.Redirect(Request.Path);
        }
    }
}